//
//  THCpcHallCell.m
//  YJB
//
//  Created by YJB on 2016/11/26.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THCpcHallCell.h"
#import "masonry.h"
#import "THCpcHallModel.h"
#import "THCpcMyPublishModel.h"
#import "THCpcMyJoinModel.h"

NSString *const cpcHallCellID = @"cpcHallCellID";
@interface THCpcHallCell ()

@end

@implementation THCpcHallCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        //self.backgroundColor = kUIColorFromRGB(0xffebe7);
        [self cpcTest];
    }
    return self;
}

- (void)cpcTest
{
    self.iconImgView.image = Image(@"th_cpc_default_normal");
    self.titleLab.text = @"只要轻松点几下，钱就到手啦，亲还在等什么呢，赶紧点我吧...";
    self.award.text = @"12金币";
    [self.vipAwardBtn setTitle:@"16金币" forState:UIControlStateNormal];
    self.surplus.text = @"剩余1000金币";
    

//    self.vipAwardBtn.backgroundColor = [UIColor orangeColor];
//    self.vipAwardBtn.imageView.backgroundColor = [UIColor yellowColor];
//    self.vipAwardBtn.titleLabel.backgroundColor = [UIColor redColor];

    
}

-(UIImageView *)iconImgView
{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.image = Image(@"th_cpc_default_normal");
        [self addSubview:_iconImgView];
        [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(20);
            make.left.equalTo(self.mas_left).offset(15);
            make.width.height.mas_equalTo(48);
        }];
    }
    return _iconImgView;
}

-(UILabel *)titleLab
{
    if (_titleLab == nil)
    {
        _titleLab = [[UILabel alloc]init];
        _titleLab.backgroundColor = [UIColor clearColor];
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.numberOfLines = 0;
        _titleLab.textColor = kUIColorFromRGB(0x333333);
        [self addSubview:_titleLab];
        [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconImgView.mas_top).offset(-2);
            make.left.equalTo(self.iconImgView.mas_right).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(32);
        }];
    }
    return _titleLab;
}

-(UILabel *)award
{
    if (_award == nil)
    {
        _award = [[UILabel alloc]init];
        _award.backgroundColor = [UIColor clearColor];
        _award.font = [UIFont boldSystemFontOfSize:16];
        _award.numberOfLines = 0;
        _award.textColor = kUIColorFromRGB(0xfa5f3c);
        [self addSubview:_award];
        NSString *award = [NSString stringWithFormat:@"100000金币"];CGFloat awardWidth = [NSString widthForString:award UIfontSize:[UIFont boldSystemFontOfSize:16] andHeight:15];
        [_award mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLab.mas_bottom).mas_offset(15);
            make.left.equalTo(self.titleLab.mas_left);
            make.width.mas_equalTo(awardWidth);
            make.height.mas_equalTo(20);
        }];
    }
    
    
    return _award;
}

-(UILabel *)surplus
{
    if (_surplus == nil)
    {
        _surplus = [[UILabel alloc]init];
        _surplus.font = FONT(14);
        _surplus.textColor = kUIColorFromRGB(0xfa5f3c);
        _surplus.textAlignment = NSTextAlignmentCenter;
        [_surplus.layer setMasksToBounds:YES];
        [_surplus.layer setCornerRadius:5.0];
        [_surplus.layer setBorderWidth:1];
        [_surplus.layer setBorderColor:kUIColorFromRGB(0xfa5f3c).CGColor];
        [self addSubview:_surplus];
        [_surplus mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.award.mas_left);
            make.width.mas_equalTo(100);
            make.height.mas_equalTo(self.vipAwardBtn.mas_height);
            make.top.equalTo(self.award.mas_bottom).offset(10);
            make.bottom.mas_equalTo(-12);
        }];
    }
    return _surplus;
}
- (UIButton *)vipAwardBtn
{
    if (!_vipAwardBtn) {
        _vipAwardBtn = [[UIButton alloc] init];
        [_vipAwardBtn setTitleColor:kUIColorFromRGB(0xfa5f3c) forState:UIControlStateNormal];
        _vipAwardBtn.titleLabel.font = FONT(14);
        _vipAwardBtn.adjustsImageWhenHighlighted = NO;
        [_vipAwardBtn setImage:Image(@"th_vip") forState:UIControlStateNormal];
        _vipAwardBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
                [_vipAwardBtn setTitle: @"16金币" forState:UIControlStateNormal];
        [self addSubview:_vipAwardBtn];
        [_vipAwardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.award.mas_centerY);
            make.left.equalTo(self.award.mas_right).offset(40);
            make.width.mas_equalTo(120);
            make.height.mas_equalTo(28);
        }];
        _vipAwardBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0,0);
    }
    return _vipAwardBtn;
}

-(void)setModel:(THCpcHallModel *)model
{
    NSString *award = [NSString stringWithFormat:@"%@金币",model.award];
    NSString *vipAward = [NSString stringWithFormat:@"%@金币",model.vipAward];
    NSString *surplus = [NSString stringWithFormat:@"剩余%@个",model.leftNum];
    
    NSMutableAttributedString *surplusAttr = [[NSMutableAttributedString alloc]initWithString:@"剩余" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0x333333)}];
    NSMutableAttributedString *leftNumAttr = [[NSMutableAttributedString alloc]initWithString:model.leftNum?model.leftNum:@"0" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0xff783c)}];
    NSMutableAttributedString *numAttr = [[NSMutableAttributedString alloc]initWithString:@"个" attributes:@{NSForegroundColorAttributeName:kUIColorFromRGB(0x333333)}];
    
    [surplusAttr appendAttributedString:leftNumAttr];
    [surplusAttr appendAttributedString:numAttr];

    
//    CGFloat titleLabWidth = IPHONE_W-15-48-15-15;
//    CGFloat titleLabelHeight = [NSString heightForString:model.title fontSize:16 andWidth:titleLabWidth];
    
    self.titleLab.text = model.title;
    self.award.text = award;
    self.surplus.attributedText = surplusAttr;
    [self.vipAwardBtn setTitle:vipAward forState:UIControlStateNormal];
    

    NSLog(@"%@",[NSString stringWithFormat:@"%@",THURL(model.iconUrl)]);
    
    NSString *urlString = model.iconUrl;
    if (![urlString hasPrefix:@"http"])
        urlString = [@"http://" stringByAppendingString:urlString];
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:Image(@"th_cpc_default_normal")];
  
    
//    [self.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(titleLabelHeight);
//    }];
    CGFloat surplusWidth = [NSString widthForString:surplus fontSize:14 andHeight:15]+12;
    CGFloat awardWidth = [NSString widthForString:award UIfontSize:self.award.font andHeight:15];
    [self.award mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLab.mas_bottom).mas_offset(2);
        make.left.equalTo(self.titleLab.mas_left);
        make.width.mas_equalTo(awardWidth);
        make.height.mas_equalTo(20);
    }];
    [self.surplus mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.award.mas_left);
        make.width.mas_equalTo(surplusWidth);
        make.height.mas_equalTo(self.vipAwardBtn.mas_height);
        make.top.equalTo(self.award.mas_bottom).offset(10);
    }];
}

-(void)setMyPublishModel:(THCpcMyPublishModel *)myPublishModel
{
    self.titleLab.text = myPublishModel.title;
    self.award.textColor = kUIColorFromRGB(0xfa5f3c);
    self.award.text = [NSString stringWithFormat:@"%@/%@",myPublishModel.adsCount,myPublishModel.completeCount];
    self.leftNum.text = myPublishModel.totalgold;
}

- (void)setMyJoinModel:(THCpcMyJoinModel *)myJoinModel
{
    self.titleLab.text = myJoinModel.title;
    self.award.textColor = kUIColorFromRGB(0xfa5f3c);
    self.award.text = myJoinModel.award;
    self.leftNum.text = [myJoinModel.createTime stringWithDateFormat:@"yyyy-MM-dd hh:mm:ss"];
}

@end
